import BaseOffice from "@/plugins/office/extend/base-office";
import api from "@/api";
import globalMethods from "@/plugins/methods";

export default class PdfOffice extends BaseOffice{
  async view(transParams, zwIframe) {
    await zwIframe.PdfAPI.initPdf({
      ...transParams,
      webRoot:`${_ctxPath}`,
      officecanSaveLocal: true,
      officecanPrint: true,
      needReadFile: true,
    });
    console.log('pdf 查询原文档')
  }

  async editOfficeTrans({zwIframe, transParams, userId,canSign}) {
    let canUse = await zwIframe.PdfAPI.canUseWebOfficeForPdf({webRoot : `${_ctxPath}`});
    let isOpen = await zwIframe.PdfAPI.isPdfOfficeAvailable();
    //TODO 已经打开了 可能不存在是多余的
    if (isOpen){
      if (!await this.validateSignature({zwIframe})){
        return;
      }
      //签章提示
      if (canUse) {
        await zwIframe.PdfAPI.initPdf({
          ...zwIframe.PdfAPI.options,
          permission: "write",
          canEdit: "true"
        });
      } else {
        await zwIframe.PdfAPI.checkPDFOpenState();
        await zwIframe.PdfAPI.pdfFullSize();
        await zwIframe.PdfAPI.modifyContent();
      }
      return;
    }
    //没有打开
    await zwIframe.PdfAPI.initPdf({
      ...transParams,
      webRoot:`${_ctxPath}`,
      officecanSaveLocal: true,
      officecanPrint: true,
      needReadFile: true,
      editType: "2,0", // 允许编辑
      actionCanSign: canSign,//签章适配
      permission: "write",
      canEdit:"true",
      callback:async()=>{
        if (!await this.validateSignature({zwIframe})){
          await zwIframe.PdfAPI.setShowModel(0);
          return;
        }
        // 金格 同步改异步
        await zwIframe.PdfAPI.checkPDFOpenState();
        await zwIframe.PdfAPI.pdfFullSize();
        await zwIframe.PdfAPI.modifyContent(canSign);
      }
    })
  }

  async editNoOfficeTrans({zwIframe,canSign}){
    await zwIframe.PdfAPI.pdfFullSize();
    await zwIframe.PdfAPI.modifyContent(canSign);//canSign  支持签章
    let canUse = await zwIframe.PdfAPI.canUseWebOfficeForPdf({webRoot : `${_ctxPath}`});
    if (canUse){
      await zwIframe.PdfAPI.initPdf({
        ...zwIframe.PdfAPI.options,
        permission: "write",
        canEdit: true,
      })

    }
    console.log('pdf 修改文档')
  }

  async validateSignature({zwIframe}) {
    if (!zwIframe.PdfAPI?.hasSign) {
      return true
    }
    let hasSign = await zwIframe.PdfAPI.hasSign();
    if (hasSign){
      globalMethods.warning({
        title: globalMethods.i18n('common.system.hint.label'),
        content: globalMethods.i18n('collaboration.alert.CantModifyBecauseOfIsignature'),
      });
      return false;
    }
    return true;
  }

  /**
   * 签章
   * @param zwIframe
   * @return {Promise<boolean>}
   */
  async signature({zwIframe}) {
    if (!zwIframe.PdfAPI) {
      return false;
    }
    let settings = await zwIframe.PdfUtils.getPluginsSettings({webRoot: `${_ctxPath}`});
    // 使用金山编辑PDF
    if (settings?.webOfficeConfig?.enabled &&
      settings?.webOfficeConfig?.kingsoftWebofficePcPDF &&
      settings?.webOfficeConfig?.type === zwIframe.PdfAPI?.defaultOptions?.webofficeTypes?.WPS) {
      if (await zwIframe.PdfUtils.webofficeSignatureJudgment({'category': '1'})) {
        // 设置金山WebOffice不可用
        settings.webOfficeConfig.kingsoftWebofficePcPDF = false;
        zwIframe.PdfUtils.setPluginsSettings({
          ...settings,
          webRoot: `${_ctxPath}`
        });
        return true;
      }
    }
    return false;
  }
}
